package com.zq.it.action.admin;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import com.zq.it.action.BaseAction;
import com.zq.it.pojo.Message;
import com.zq.it.service.admin.MessageService;
import com.zq.it.service.impl.admin.PageService;
import com.zq.it.tool.Constant;
import com.zq.it.tool.FileName;
import com.zq.it.tool.FileType;
import com.zq.it.tool.HeadTitle;

public class MessageAction extends BaseAction {
	
	/***/
	
	private static final long serialVersionUID = 1L;
	@Resource
	private MessageService messageService;
	@Resource
	private PageService pageService;
	
	private String textValue;
	private String searchId;
	private Message message;
	private String msgContent;
	private String msgIdGroups;
	

	public String getMsgIdGroups(){
		return msgIdGroups;
	}
	public void setMsgIdGroups(String msgIdGroups){
		this.msgIdGroups = msgIdGroups;
	}
	
	public String getMsgContent(){
		return msgContent;
	}
	public void setMsgContent(String msgContent){
		this.msgContent = msgContent;
	}
	public Message getMessage() {
		return message;
	}
	public void setMessage(Message message) {
		this.message = message;
	}
	public String getTextValue() {
		return textValue;
	}
	public void setTextValue(String textValue) {
		this.textValue = textValue;
	}

	public String getSearchId() {
		return searchId;
	}

	public void setSearchId(String searchId){
		this.searchId = searchId;
	}

	public MessageService getMessageSevice(){
			return messageService;
		}
//获取全部信息列表
	   
	public void getMessageList(){ 		
		List list=messageService.getAllMessages();
		uploadList(com.zq.it.tool.EtyLsName.MSG_LS.name(),list);
		print(toJSON(list),"UTF-8","application/json"); 
	   }
	
	//根据信息的标题、类型、时间来查询消息的扼要
	public void  sendMessageListByType(){		
		List list=messageService.getMessagesByType(searchId,textValue);
		uploadList(com.zq.it.tool.EtyLsName.MSG_LS.name(),list);
		print(toJSON(list),"UTF-8","application/json"); 
	}
	public void getCurrentMsg(){
	SimpleDateFormat sf=new SimpleDateFormat("YYYY-MM-dd");
       long ss=System.currentTimeMillis();
		print(toJSON(messageService.getMsgByHql("from Message where updateTime='"+sf.format(ss-86400000)+"' or updateTime='"+sf.format(ss)+"'")),"UTF-8","application/json");	
	}
	public void updateMsg(){		
		messageService.updateMessageEntity(getMsgContent(),message,getRealPath(Constant.MSG_SAVE_URL));
		print("<script>parent.closeWindow('已更新')</script>","UTF-8","text/html");
	}
	
	public void  addMsg(){		
	
      messageService.addMessage(message,getMsgContent(),getRealPath(Constant.MSG_SAVE_URL));
        print("<script>parent.closeWindow('已添加');</script>","UTF-8","text/html");
	}	
	public void deleteMessageEntity(){
	  messageService.removeMessage(message,getRealPath(Constant.MSG_SAVE_URL));
	  print("已删除一条记录！","UTF-8","text/html");
	}
	public void deleteMessageGroups(){
		
		getMessageSevice().removeMessageByGroups(msgIdGroups);
		print("已删除"+getMsgIdGroups().split(":").length+"记录！","UTF-8","TEXT/HTML");		
	}
	public void importMsg(){	
		String msgStr="";
		List<Message> list=messageService.importMsgList(getUpload());
		if(list.size()>0){
		 getRequest().getSession().setAttribute("mexistList",list);	
		 msgStr="existList";
		}else{
			msgStr="已导入成功!";
		}
		print("<script>parent.importBackResult('"+msgStr+"','webpage/adminpage/msg/existMsg.jsp');</script>","UTF-8","TEXT/HTML");
	}
	public void exportMsg(){				
		download(messageService.exportMsgList(HeadTitle.CN_MSGHEAD.getArray(),(List)getHttpSession().getAttribute(com.zq.it.tool.EtyLsName.MSG_LS.name())),FileName.MSG_LIST_NAME.toString()+FileType.XLSX);
	}
	
	public void getMsgByTitle(){
		
		messageService.getMessageByTitle(textValue);
	}
	
	public String getMsgByPage(){	
		List list=null;
		if(type==null&&(time==null||"".equals(time))){
			 list=pageService.getPageEtyList(getSelectedNum(),"from Message");
		}
		else{
			if(time!=null&&!"".equals(time)){			
				list=pageService.getPageEtyList(getSelectedNum(),"from Message where updateTime='"+time+"'");
			}			    
			if(type!=null){
				System.out.println(type);
				list=pageService.getPageEtyList(getSelectedNum(),"from Message where type="+type);	
			}							
		}
						
        int size=list.size();
        System.out.println(size);
        size=(Integer)list.get(size-1);
		list.remove(list.size()-1);			
		int totalPage=size%30==0?size/30:size/30+1;
		 setPage("message_getMsgByPage.asp",totalPage,type,time);				 
		 saveListToRequest("msglist",list);
		 return "msgPage";
	}
	
	private Integer type=null;
	private String time=null;
	
	public int getType() {
		return type;
	}
	public void setType(Integer type){
		this.type = type;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		
		this.time = time;
	}
	
}
